/*******************************************************************************
 * Copyright (c) 2014 Francesco Gabbrielli
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Francesco Gabbrielli - initial API and implementation
 ******************************************************************************/
package it.francescogabbrielli.android.solidalapp.response;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import it.francescogabbrielli.android.solidalapp.EndpointsUtils;
import it.francescogabbrielli.android.solidalapp.R;
import it.francescogabbrielli.android.solidalapp.SolidalApp;
import it.francescogabbrielli.android.solidalapp.SolidalApp.DataKey;
import it.francescogabbrielli.android.solidalapp.requestendpoint.model.Request;
import it.francescogabbrielli.android.solidalapp.responseendpoint.model.Response;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockDialogFragment;

public class RequestDialogFragment extends SherlockDialogFragment {

	private Request request;
	private Bundle requestBundle;
	
	private ResponseFragmentListener listener;

	public RequestDialogFragment() {
	}

	public RequestDialogFragment setRequest(Request request) {
		this.request = request;
		return this;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		listener = (ResponseFragmentListener) getActivity();		
	}
		
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		LayoutInflater inflater = getActivity().getLayoutInflater();

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

		View view = inflater.inflate(R.layout.dialog_request, null);

		int title = R.string.request_dialog_title_ok;
		
		// update request
		if (request!=null) {
			requestBundle = EndpointsUtils.toBundle(request);
			requestBundle.putLong("requestId", request.getId());
		} else if(savedInstanceState!=null)
			requestBundle = savedInstanceState.getBundle("request");
		
		
		if(requestBundle!=null) {
			
			SolidalApp app = (SolidalApp) getActivity().getApplication();
			Response response = (Response) app.getData(DataKey.RESPONSE);
			boolean current = response!=null && response.getRequestId()==requestBundle.getLong("requestId");
			
			SimpleDateFormat dateFormat = new SimpleDateFormat(
					getString(R.string.simple_dateformat),
					Locale.getDefault());
			((TextView) view.findViewById(R.id.dialog_request_message))
					.setText(requestBundle.getString("message"));
			((TextView) view.findViewById(R.id.dialog_request_sender))
					.setText(requestBundle.getString("sender") != null ? requestBundle.getString("sender") : getString(R.string.anonymous_user));
			((TextView) view.findViewById(R.id.dialog_request_address))
					.setText(requestBundle.getString("details"));
			((TextView) view.findViewById(R.id.dialog_request_time))
					.setText(dateFormat.format(new Date(requestBundle.getLong("timestamp"))));
			((TextView) view.findViewById(R.id.dialog_request_expiry))
					.setText(dateFormat.format(
							new Date(
								requestBundle.getLong("timestamp")
								+ requestBundle.getInt("duration") * 60000L)));
			((TextView) view.findViewById(R.id.dialog_request_credits))
					.setText(String.valueOf(requestBundle.getInt("credits")));
			
			boolean expired = System.currentTimeMillis() > requestBundle.getLong("expiry");
			
			if (current) {
				title = R.string.request_dialog_title_accepted;
			} else if (requestBundle.getBoolean("completed")) {
				title = R.string.request_dialog_title_completed;
			} else if (requestBundle.getBoolean("accepted")) {
				title = R.string.request_dialog_title_busy;
			} else if (requestBundle.getBoolean("canceled")) {
				title = R.string.request_dialog_title_canceled;
			} else if (expired || requestBundle.getBoolean("expired")) {
				title = R.string.request_dialog_title_expired;
			} else {
				builder.setPositiveButton(R.string.request_dialog_accept,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int id) {
							listener.showAcceptDialog(
									EndpointsUtils.fromBundle(requestBundle, Request.class));
						}
					});
			}
			
		}
		
		builder
			.setView(view)
			.setTitle(title);
	
		// Create the AlertDialog object and return it
		return builder.create();
	}

	@Override
	public void onSaveInstanceState(Bundle data) {
		data.putBundle("request", requestBundle);
		super.onSaveInstanceState(data);
	}

}
